public class ContactPhoneTriggerHandler{
        static Set<Id> conphoneupdateid=new Set<Id>();
        static Set<Id> conphoneinsertid=new Set<Id>();
    public void ContactPhoneInsert(List<Contact_Phone__c> conphonelist)    {
             Set<Id> phoneid=new Set<Id>();
             Set<Id> accid=new Set<Id>();
             
             List<Contact_Phone__c> oldcontactphonelist=new List<Contact_Phone__c>();
             List<Contact_Phone__c> oldcontactphoneupdatelist=new List<Contact_Phone__c>();
             Contact_Phone__c phone=new Contact_Phone__c();
          if(conphonelist!=null&&conphonelist.size()>0){
          phone = conphonelist[0];
              for(Contact_Phone__c conphoneval: conphonelist){
                  phoneid.add(conphoneval.id);
                  accid.add(conphoneval.Contact__c);
                  
              }
             
            oldcontactphonelist=[select Contact__c,Preferred__c,Phone_Type__c from Contact_Phone__c where Preferred__c=true AND Contact__c IN:accid AND Id NOT IN:phoneid];
               
          
           for(Contact_Phone__c conphone:conphonelist){
               // To insert country code..
                    if(conphone.Country__c!=null){
                        String country=conphone.Country__c;
                        String c1=country.substring((country.indexOf('(')+1),country.indexOf(')'));
                        conphone.Country_Name__c=c1;
                    } 
            if(oldcontactphonelist!=null && oldcontactphonelist.size()>0){
                                
                 
                     system.debug('@@Preferred'+ (conphone.Preferred__c));
                        if(conphone.Preferred__c==true){                        
                            for(Contact_Phone__c oldconphone: oldcontactphonelist){
                                oldconphone.Preferred__c=false;
                                oldcontactphoneupdatelist.add(oldconphone);
                                conphoneinsertid.add(oldconphone.Id);
                            }
                        }
                 
                 }else{
              
                          phone .Preferred__c=true;
                }
              
             }
                if(!oldcontactphoneupdatelist.IsEmpty()){
                          update oldcontactphoneupdatelist;
                      }
              
                  }
         }
    public void ContactPhoneUpdate(List<Contact_Phone__c> newconphonelist,List<Contact_Phone__c> oldconphonelist){
        Set<Id> phoneid=new Set<Id>();
        Set<Id> accid=new Set<Id>();
       
        List<Contact_Phone__c> oldcontactphonelist=new List<Contact_Phone__c>();
        List<Contact_Phone__c> oldcontactphoneupdatelist=new List<Contact_Phone__c>();
        
        if(newconphonelist!=null&&newconphonelist.size()>0){    
            for(Contact_Phone__c conphoneval: newconphonelist){
                phoneid.add(conphoneval.Id);
                accid.add(conphoneval.Contact__c);
                
            }   
          
        oldcontactphonelist=[select Contact__c,Preferred__c,Phone_Type__c from Contact_Phone__c where Preferred__c=true AND Contact__c IN:accid  AND Id NOT IN:phoneid];
              
         for(Contact_Phone__c newconphone: newconphonelist){  
         	      // To insert country code..
                     if(newconphone.Country__c!=null){
                        String country=newconphone.Country__c;
                        String c1=country.substring((country.indexOf('(')+1),country.indexOf(')'));
                        newconphone.Country_Name__c=c1;
                    }           
                for(Contact_Phone__c oldconphone: oldconphonelist){
                system.debug('##'+(newconphone.Preferred__c));
                    if((newconphone.Preferred__c==true && oldconphone.Preferred__c==false&&newconphone.Contact__c==oldconphone.Contact__c)){ 
                        system.debug('INSIDEIF');
                        if(oldcontactphonelist!=null && oldcontactphonelist.size()>0){
                        for(Contact_Phone__c convalue: oldcontactphonelist){
                              convalue.Preferred__c=false;        
                              oldcontactphoneupdatelist.add(convalue);
                              conphoneupdateid.add(convalue.Id);                            
                          }
                        } 
                    } else{
                    system.debug('INELSE');
                    system.debug('@@conphoneupdateid '+conphoneupdateid+'&&conphoneinsertid'+ conphoneinsertid);
                    for(Contact_Phone__c accval: newconphonelist){
                          
                            if(conphoneupdateid.contains(accval.id) ){
                            }else{
                            if(conphoneinsertid.IsEmpty()&&(newconphone.Preferred__c==false && oldconphone.Preferred__c==true))
                             trigger.new[0].addError('cannot update');  
                            }
                        }
                    
                    }               
                 }
                }    
                if(!oldcontactphoneupdatelist.isEmpty()){
                    update oldcontactphoneupdatelist;
                }                  
          }
    }
    public void ContactPhoneDelete(List<Contact_Phone__c> contactphonelist){
        if(contactphonelist!=null&&contactphonelist.size()>0){
            system.debug('@@accountemaillist '+contactphonelist);
            for(Contact_Phone__c conphone: contactphonelist){
            
            if(conphone.Preferred__c==true){
                    trigger.old[0].addError('Cannot delete prefered record');  
                }
            }
        
        }

    }
    
/************************************************************************
        Purpose     : Test Class                  
        Parameters  : None
        Returns     : None
***********************************************************************/
    @isTest(SeeAllData=false)
     static public void testContactPhoneTriggerHandler() {
     
         PIMS_Reserved_Id__c pimsid=new PIMS_Reserved_Id__c();
         pimsid.PIMS_Id__c='0000-0000';
         pimsid.Used__c=false;
         insert pimsid;
         
         PIMS_Reserved_Id__c pimsid1=new PIMS_Reserved_Id__c();
         pimsid1.PIMS_Id__c='0001-0000';
         pimsid1.Used__c=false;
         insert pimsid1;
         
         Account acc=new Account();
         acc.Name='test';
         acc.Type='SSO';
         insert acc;
         
         
         Contact con=new Contact();
         con.LastName='Test';
         con.AccountId=acc.id;
         con.Status__c='Active';
         con.Affiliate_Organizational_Role__c='test';
         con.Gender__c='Male';
         insert con;
         
         Contact_Phone__c conphone=new Contact_Phone__c();
         conphone.Contact__c=con.id;
         conphone.Country__c='Canada (CA)';
         conphone.Phone__c='12345';
         conphone.Phone_Type__c='Fax';
         conphone.Preferred__c=true;
         insert conphone;
         update conphone;
         
         Contact_Phone__c conphone0=new Contact_Phone__c();
         conphone0.Contact__c=con.id;
         conphone0.Country__c='Canada (CA)';
         conphone0.Phone__c='12345';
         conphone0.Phone_Type__c='Fax';
         conphone0.Preferred__c=true;
         insert conphone0;
         
         Contact_Phone__c conphone1=new Contact_Phone__c();
         conphone1.Contact__c=con.id;
         conphone1.Country__c='Canada (CA)';
         conphone1.Phone__c='12345';
         conphone1.Phone_Type__c='home';
         conphone1.Preferred__c=false;
         insert conphone1;
         conphone1.Preferred__c=true;
         update conphone1;
         
         Contact_Phone__c conphone2=new Contact_Phone__c();
         conphone2.Contact__c=con.id;
         conphone2.Country__c='Canada (CA)';
         conphone2.Phone__c='12345';
         conphone2.Phone_Type__c='Fax';
         conphone2.Preferred__c=false;
         insert conphone2;
         delete conphone2;
         
        }
}